<?php

class AgendaController extends Site_Controller_Action
{
    public function init()
    {
        $this->body_class = 'no-sidebar';
    }

    public function indexAction()
    {
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/mapa.js');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/jquery-ui-1.10.2.custom/css/custom-theme/jquery-ui-1.10.2.custom.css'); 
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery-ui-1.10.2.custom/js/jquery-ui-1.10.2.custom.min.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/conflict.js');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/bootstrap/dist/css/bootstrap-theme.min.css');
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/css/bootstrap.tooltip.css');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/bootstrap/dist/js/bootstrap.min.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskMoney.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/mask.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/agenda.js');
        
        
        $result = $this->db->fetchAll('SELECT SQL_CACHE b.*,c.CTS_NOM FROM ST_SERVICO b, ST_CATEGORIA_SERVICO c WHERE b.CTS_ID = c.CTS_ID AND SER_STA = 1 ORDER BY SER_NOM ASC');
        $this->view->servicos = $result; 
    }
    
    public function agendarAction()
    {
        Zend_Loader::loadClass('datetools','library');
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();
        
        $ok = false;
        
        if($this->getRequest()->isPost()) {
            try {
                $formData = $this->getRequest()->getPost();
                
                $data = array(
                    'AGD_CLINOM'=> $formData['nome'],
                    'AGD_CLITEL'=> $formData['telefone'],
                    'AGD_DAT'   => Datetools::dma_amd($formData['data']),
                    'AGD_HOR'   => $formData['hora'],
                    'SER_ID'    => $formData['servico'],
                    'AGD_MSG'   => $formData['mensagem'],
                    'AGD_STA'   => 1,
                );
                
                $this->db->insert('ST_AGENDA', $data);
                
                $ser = $this->db->fetchRow('SELECT SQL_CACHE b.SER_NOM,c.CTS_NOM FROM ST_SERVICO b, ST_CATEGORIA_SERVICO c WHERE SER_ID = '.$formData['servico'].' AND b.CTS_ID = c.CTS_ID');
                
                Zend_Loader::loadClass('envioemail','library');
                $arr['nome']        = $formData['nome'];
                $arr['telefone']    = $formData['telefone'];
                $arr['data']        = Datetools::dma_amd($formData['data']);
                $arr['hora']        = $formData['hora'];
                $arr['servico']     = $ser['CTS_NOM'].' '.$ser['SER_NOM'];
                $arr['mensagem']    = $formData['mensagem'];
                $arr['urlbase']     = $this->_urlbase;
                $arr['modulo']      = '';//contato';
                $ok = envioemail::agendaHorario($arr);
                
                $ok = true;
            } catch (Exception $exc) {
                //echo 'error: '.$exc->getMessage();
                $ok = false;
            }
        }
        
        echo json_encode(array('ok' => $ok));
    }
}

